{{define "cms/chang_pwd.html"}}
{{ template "header" . }}

<body class="gray-bg">
<div class="wrapper wrapper-content">
    <div class="ibox-title">
        个人设置 / 密码修改
    </div>
    <div class="row">
        <div class="col-sm-12">
            <div class="ibox float-e-margins">
                <div class="ibox-content">
                    <div class="form-horizontal">
                        <input type="hidden" name="token" value="{{ .token }}">
                        <div class="form-group">
                            <label class="col-sm-2 control-label">旧密码：</label>
                            <div class="col-sm-3">
                                <input type="password" name="old_passwd" class="form-control" placeholder="" required><span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">新密码：</label>
                            <div class="col-sm-3">
                                <input type="password" name="passwd" class="form-control" placeholder="" required><span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <label class="col-sm-2 control-label">确认新密码：</label>
                            <div class="col-sm-3">
                                <input type="password" name="confirm_passwd" class="form-control" placeholder="" required><span class="help-block m-b-none"></span>
                            </div>
                        </div>
                        <div class="form-group">
                            <div class="col-sm-offset-2 col-sm-3">
                                <button class="btn btn-sm btn-primary" onclick="changePwd()">修 改</button>
                            </div>
                        </div>
                    </div>
                </div>
            </div>
        </div>
    </div>
</div>
</body>


{{ template "footer" . }}
<script>
    function changePwd(){
        var oldPwd = $("input[name='old_passwd']").val();
        var pwd = $("input[name='passwd']").val();
        var token = $("input[name='token']").val();
        var confirmPwd = $("input[name='confirm_passwd']").val();
        var prompt_msg = "";
        if(oldPwd === ""){
            prompt_msg = "旧密码不能为空";
        }else if(pwd === ""){
            prompt_msg = "新密码不能为空";
        }else if(confirmPwd === ""){
            prompt_msg = "确认新密码不能为空";
        }
        if (prompt_msg !== ""){
            layer.msg(prompt_msg, {icon: 2, time: 1200, title: "提示"});
            return;
        }
        $.post("/pwdedit", {old_passwd:oldPwd, passwd:pwd, confirm_passwd:confirmPwd, token: token},function (res) {
            var jsonData = $.parseJSON(res);
            if(jsonData.status !== 200){
                if(jsonData.status !== 20007) {
                    $("input[name='token']").val(jsonData.data);
                }
                layer.msg(jsonData.message, {icon: 2, time: 1200, title: "提示"});
            }else{
                layer.msg(jsonData.message, {
                    icon: 1,
                    time: 1000,
                    title: "提示",
                    end:function () {
                        $.get("/logout",function (res) {
                            var json = $.parseJSON(res);
                            if (json.status === 200){
                                window.location.href = "/login";
                            }
                        });
                    }
                });
            }
        });
    }
</script>
{{end}}